package Controler;

import java.util.ArrayList;

import Client.BusinessImplements;
/**
* @author Lucie
 *class Create_memmber_card_controler
 *Class variables:msg,flag
 *Class methods:set_new_member_data,check_input,String get_msg,set_msg
 */
public class Create_memmber_card_controler {

	private String msg; 
	private boolean flag;
	/**
	 *  Constructor initializes the class variables
	 */
	public Create_memmber_card_controler()
	{
		flag=true;
		msg=new String();
		msg=null;
	}
	
	/**
	 * 
	 * @return String white all of the problems that occurred in the class
	 */
	public String get_msg()
	{
		return msg;
	}
	/**
	 * This method chain messages into a string ,
	 * the purpose ultimately is to display on the screen all the failures
	 * @param new_msg
	 */
	public void set_msg(String new_msg)
	{
		if (msg==null)
			msg=new_msg;
		else
		msg+=new_msg;
		
	}
	
	
/**
 * 
 * @param id
 * @param name
 * @param last_name
 * @param phone
 * @param email
 * @param status
 * this  method call the class ;serverproxy, to Contact with the server,
 * first we check the Parameters we get (check_input) if something is wrong we return false 
 *than we call the method getUserdetails that check if the user already exist and if he thus exist we return false
 *and set an error msg  if everything sucsed  we add the user to the db (addUser) and add reader card to the db (addReaderCard)if something go wrong we return false 
 * @return  boolean:If the calls in the method failed it returns false otherwise returns true
	 */
 
	public boolean set_new_member_data(String id, String name,String last_name,String phone, String email,String status)
	{
		 ArrayList<String> str_readercard= new  ArrayList<String>();
		 ArrayList<String> str_check_user= new  ArrayList<String>();
		 ArrayList<String> str_user= new  ArrayList<String>();
		 
		 check_input( id,name, last_name,phone,email,status);
		 if (flag==false)
			 return false;
		 
		 str_check_user=BusinessImplements.GetInstance().getServerProxy().checkUserexist(id);
		 System.out.println(str_check_user.get(0));
		 if (str_check_user.get(0)==null)
		 {
			
			 str_user=BusinessImplements.GetInstance().getServerProxy().addUser(id, email, "1234", "1");
			 System.out.println(str_user.get(0));
			 	if ( str_user.get(0).equals("false"))
			 	{
			 		
			 			set_msg("\nadd user faild "); 
			 			return false;
		 		}
			 	
			 	str_readercard=BusinessImplements.GetInstance().getServerProxy().addReaderCard(id,name, last_name,phone,status);
			 	System.out.println(str_readercard.get(0));
			 	if ( str_readercard.get(0).equals("false"))
			 	{
			 		set_msg("\nadd memmberd card faild "); 
			 		return false;
			 	}
		 }
		 else
		 {
			 set_msg("\nmemmber allredy exist in the system"); 
		 		return false; 
		 }
		 
			return flag;
	}
	

	/**
	 * 
	 * @param id
	 * @param name
	 * @param last_name
	 * @param phone
	 * @param email
	 * @param status
	 *  This method checks the parameters we have received are not empty
	 * if they are poses flag= false;
	 */
	
	public void check_input(String id, String name,String last_name,String phone, String email,String status)
	{
		
		if(id.equals(""))
		{
			set_msg("\nmising id number ");
			flag=false;
		}
		
		if(name.equals(""))
		{
			set_msg("\nmissing name ");
			flag=false;
		}
		
		if(last_name.equals(""))
		{
			set_msg("\nmissing last name ");
			flag=false;
		}
		
		if(phone.equals(""))
		{
			set_msg("\nmissing phone ");
			flag=false;
		}
		if(email.equals(""))
		{
			set_msg("\nmissing email ");
			flag=false;
		}
		if(status.equals(""))
		{
			set_msg("\nmissing status ");
			flag=false;
		}
		
	
	}

	
		
}
